package org.itboys.mobile.service.mongo.weixin;

import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.collection.SundayCollectionDto;
import org.itboys.mobile.entity.mongo.SundayActivity;
import org.itboys.mobile.entity.mongo.SundayCollection;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayActivityService;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mobile.service.mysql.weixin.SundayWeiXinProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author：贾琦磊
 * @Date：2018/7/11
 * @Description：收藏
 */
@Service
public class SundayWeiXinCollectionService extends BaseMongoService<SundayCollection> {
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayCollection> getEntityClass() {
        return SundayCollection.class;
    }

    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayActivityService activityService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Autowired
    private SundayWeiXinProductService weiXinProductService;

    /**
     * 用户收藏活动,返回用户是否收藏
     * @param memberId
     * @param otherId
     * @param type
     */
    public Integer save(Long memberId,Long otherId,Integer type){
        Map param=new HashMap();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("type",type);
        param.put("memberId",memberId);
        param.put("otherId",otherId);
        List<SundayCollection> collects = super.list(param);
        if(ListUtil.isNotNull(collects)){
            super.delete(collects.get(0).getId());
            return SundayCommonConstants.NO;
        }else {
            SundayCollection collection=new SundayCollection(memberId,type,otherId);
            super.save(collection);
            return  SundayCommonConstants.YES;
        }
    }

    /**
     * 查询用户收藏
     * @param ids
     * @param type
     * @return
     */
    public List<SundayCollection> select(Set<Long> ids, Integer type){
        HashMap param=new HashMap();
        param.put("otherId in",ids);
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("type",type);
        return  super.list(param);
    }


    /**
     * 罗尧坤添加
     * 查询单个用户的所有关注产品
     * @param request
     * @param memberId
     * @return
     */
    public List<SundayCollectionDto> selectList(HttpServletRequest request, Long memberId){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("memberId", memberId);
        param.put("sort","ct");
        param.put("order","desc");
        param.put("isDeleted", 0);

        //查询出当前用户的收藏记录
        List<SundayCollection> data = super.pageQuery(request,param).getData();

        if(ListUtil.isNull(data)){
            return new ArrayList<>();
        }

        List<SundayCollectionDto> result = new ArrayList<>();

        List<Long> proIds = new ArrayList<>();
        List<Long> actIds = new ArrayList<>();

        for (SundayCollection collection :data) {
            //查询活动ids
            if (collection.getType() == SundayCommonConstants.collect_type_activity){
                actIds.add(collection.getOtherId());
            }
            //查询楼盘ids
            else if (collection.getType() == SundayCommonConstants.collect_type_product){
                proIds.add(collection.getOtherId());
            }
            //意外情况
            else {

            }
        }

        Map<String,Object> _param= new HashMap<>();
        _param.put("ids",proIds);
        List<SundayProduct> products = productService.select(_param) ;


        Map<String, Object> a_param = new HashMap<>();
        a_param.put("isDeleted", 0);
        a_param.put("id in", actIds);
        List<SundayActivity> activities = activityService.list(a_param);

        for (SundayCollection collection :data) {
            //储存活动详情
            if (collection.getType() == SundayCommonConstants.collect_type_activity){
                for (SundayActivity activity :activities) {
                    if(collection.getOtherId().equals(activity.getId())){
                        SundayCollectionDto dto = packageDto(collection,activity);
                        result.add(dto);
                        break;
                    }
                }
            }
            //查询楼盘详情
            else if (collection.getType().equals(SundayCommonConstants.collect_type_product)){
                for (SundayProduct product :products) {
                    if(collection.getOtherId().equals(product.getId())){
                        SundayCollectionDto dto = packageDto(collection,product);
                        result.add(dto);
                        break;
                    }
                }
            }
            //意外情况
            else {

            }
        }
        return result;
    }

    /**
     * 包装DTO: 楼盘
     * @param collection
     * @param product
     * @return
     */
    public SundayCollectionDto packageDto(SundayCollection collection, SundayProduct product ){
        SundayCollectionDto dto = new SundayCollectionDto();
        dto.setId(collection.getId());
        dto.setType(collection.getType());
        dto.setCollectionId(collection.getOtherId());
        dto.setCollectionName(product.getName());
        dto.setCollectionCategory(product.getCategoryName());
        dto.setBackImage(product.getBackImage());
        dto.setPriceRange(product.getTotalPrice());
        dto.setAcreage(product.getAcreage());

        //封装标签
        dto.setTags(getLable(product));
        return dto;
    }

    /**
     * 包装Dto: 活动
     * @param collection
     * @param activity
     * @return
     */
    public SundayCollectionDto packageDto(SundayCollection collection, SundayActivity activity ){
        SundayCollectionDto dto = new SundayCollectionDto();
        dto.setId(collection.getId());
        dto.setType(collection.getType());
        dto.setCollectionId(collection.getOtherId());
        dto.setCollectionName(activity.getName());
        dto.setBackImage(activity.getHeadImage());
        dto.setLable(activity.getSlogan());
        dto.setsDate(activity.getsDate());
        dto.seteDate(activity.geteDate());

        return dto;
    }

    /**
     * 查询标签
     * @param product
     * @return
     */
    public List<Map<String, Object>> getLable(SundayProduct product) {
        //组装标签
        List<SystemDictionary> dictionaries = dictionaryService.getDictionaryByKey("house_tag");

        if (product.getTag() == null) product.setTag("");
        String[] tagIds = product.getTag().split(",");
        List<Map<String, Object>> tags = new ArrayList<>();

        for (SystemDictionary dictionary : dictionaries) {
            Map<String, Object> tag = new HashMap<String, Object>();
            tag.put("id", dictionary.getId());
            tag.put("name", dictionary.getValue());
            int isChecked = 0;
            for (String tagId : tagIds) {
                if (tagId.equalsIgnoreCase(String.valueOf(dictionary.getId()))) {
                    isChecked = 1;
                    break;
                }
            }
            tag.put("isChecked", isChecked);
            tags.add(tag);
        }

        return tags;
    }

}
